package com.spacex.danhuang.polive.fragments;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.spacex.danhuang.polive.Application.VApplication;
import com.spacex.danhuang.polive.Constants.Constants;
import com.spacex.danhuang.polive.R;
import com.spacex.danhuang.polive.activities.MediaRecActivity;
import com.spacex.danhuang.polive.adapters.MainPageFragmentAdapter;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.socket.client.Socket;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainPageFragment extends AppBaseFragment {
    private static final String TAG = "MainPageFragment";

    private VApplication app;
    private Socket mSocket;

    private ViewPager vp_fragment_main_page;
    private TextView tv_hot_tab_of_main_page;
    private TextView tv_all_tab_of_main_page;
    private TextView tv_concern_tab_of_main_page;

    private static MainPageFragment mainPageFragment = null;
    private HotShowFragment hotShowFragment = null;
    private AllShowFragment allShowFragment = null;
    private MyConcernFragment myConcernFragment = null;

    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private MainPageFragmentAdapter mainPageFragmentAdapter;

    private int currentIndex;

    private String mRoomName;
    private int mUserRole;

    public MainPageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_main_page, container, false);
        vp_fragment_main_page = (ViewPager)viewGroup.findViewById(R.id.vp_fragment_main_page);
        tv_hot_tab_of_main_page = (TextView)viewGroup.findViewById(R.id.tv_hot_tab_of_main_page);
        tv_all_tab_of_main_page = (TextView)viewGroup.findViewById(R.id.tv_all_tab_of_main_page);
        tv_concern_tab_of_main_page = (TextView)viewGroup.findViewById(R.id.tv_concern_tab_of_main_page);

        app = (VApplication)getActivity().getApplication();
        mSocket = app.getSocket();
        mUserRole = app.getUserRole();

        initFragment();

        return viewGroup;
    }

    private void initFragment() {
        hotShowFragment = HotShowFragment.getInstance();
        allShowFragment = AllShowFragment.getInstance();
        myConcernFragment = MyConcernFragment.getInstance();

        fragmentList.add(hotShowFragment);
        fragmentList.add(allShowFragment);
        fragmentList.add(myConcernFragment);

        mainPageFragmentAdapter = new MainPageFragmentAdapter(getChildFragmentManager(), fragmentList);
        vp_fragment_main_page.setAdapter(mainPageFragmentAdapter);

        currentIndex = Constants.MAIN_PAGE_FRAGMENT_TAB_HOTSHOW;
        vp_fragment_main_page.setCurrentItem(currentIndex);
        setCurrentItemStyle(currentIndex);

        vp_fragment_main_page.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentItemStyle(position);
                currentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setCurrentItemStyle(int position) {
        resetTextView();
        setSelectPageContent(position);
    }

    private void resetTextView() {
        tv_hot_tab_of_main_page.setTextColor(Color.BLACK);
        tv_all_tab_of_main_page.setTextColor(Color.BLACK);
        tv_concern_tab_of_main_page.setTextColor(Color.BLACK);
    }

    private void setSelectPageContent(int position) {
        switch (position) {
            case Constants.MAIN_PAGE_FRAGMENT_TAB_HOTSHOW:
                tv_hot_tab_of_main_page.setTextColor(Color.BLUE);
                break;
            case Constants.MAIN_PAGE_FRAGMENT_TAB_ALLSHOW:
                tv_all_tab_of_main_page.setTextColor(Color.BLUE);
                break;
            case Constants.MAIN_PAGE_FRAGMENT_TAB_CONCERN:
                tv_concern_tab_of_main_page.setTextColor(Color.BLUE);
                break;
        }
    }

    @Override
    protected void handleSwitchMethod(String rsp_name, int rsp_status, String rsp_errmsg, JSONObject onHandleMsgData) {
        switch (rsp_name) {
            case Constants.JOIN_ROOM_NAME:
                Log.i(TAG, "handleJoinRoom");
                handleJoinRoom(rsp_status, rsp_errmsg);
                break;
            default:
                Log.i(TAG, "no rsp name");
        }
    }

    private void handleJoinRoom(final int status, final String errmsg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (status != 0) {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "ERROR"+errmsg, Toast.LENGTH_SHORT).show();
                    return;
                }

                mRoomName = app.getRoomName();

                //跳转至直播间
                Intent intent = new Intent(getActivity(), MediaRecActivity.class);
                intent.putExtra("roomName", mRoomName);
                if (mUserRole == Constants.USER_COMMON) {
                    intent.putExtra("chatroomRole", Constants.USER_CHATROOM_VISITOR);
                } else if (mUserRole == Constants.USER_MANAGER) {
                    intent.putExtra("chatroomRole", Constants.USER_CHATROOM_MANAGER);
                }
                startActivity(intent);
            }
        });
    }
}
